package com.eqgis.eqtool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.ArrayList;

public class TestView extends SurfaceView {

    private Paint paint;
    private Paint paint2;
    private boolean open = true;

    public TestView(Context context) {
        super(context);
        init();
    }

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(((0xff << 24) | (255<<16) | (255<<8) | 2));
        paint.setStrokeWidth(5);
        paint2 = new Paint();
        paint2.setColor(((200 << 24) | (176<<16) | (156<<8) | 49));
        paint2.setStrokeWidth(5);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
    }


    public void update(ArrayList<int[]> points){
        if (!this.open){
            return;
        }
        SurfaceHolder holder = getHolder();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            float[] pts = new float[points.size() * 2];
            for (int[] p : points) {
                canvas.drawPoint(p[0],p[1],paint2);
            }
            for (int i = 0; i < points.size(); i++) {
                int[] ints = points.get(i);
                pts[2 * i] = ints[0];
                pts[2 * i + 1] = ints[1];
            }
            canvas.drawLines(pts,paint2);
        }catch (Exception e){}
        finally {
            holder.unlockCanvasAndPost(canvas);
        }
    }
    @SuppressLint("Range")
    public void update(Rect rect){
        if (!this.open){
            return;
        }
        SurfaceHolder holder = getHolder();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//            canvas.drawRect(rect,paint);
            float[] pts = new float[16];
            pts[0] = rect.left;
            pts[1] = rect.top;
            pts[2] = rect.right;
            pts[3] = rect.top;

            pts[4] = rect.right;
            pts[5] = rect.top;
            pts[6] = rect.right;
            pts[7] = rect.bottom;

            pts[8] = rect.right;
            pts[9] = rect.bottom;
            pts[10] = rect.left;
            pts[11] = rect.bottom;

            pts[12] = rect.left;
            pts[13] = rect.bottom;
            pts[14] = rect.left;
            pts[15] = rect.top;
            canvas.drawLines(pts,paint);
        }catch (Exception e){}
        finally {
            holder.unlockCanvasAndPost(canvas);
        }
    }
    public void clear(){
        if (!this.open){
            return;
        }
        SurfaceHolder holder = getHolder();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        }catch (Exception e){}
        finally {
            holder.unlockCanvasAndPost(canvas);
        }
    }
}
